local chuiqian = fk.CreateSkill{
  name = "lvshi__chuiqian",
  tags = {"Turn"}
}

Fk:loadTranslationTable{
  ["lvshi__chuiqian"] = "鸣谦",
  [":lvshi__chuiqian"] = "回合技，当你摸牌时，可改为亮出牌堆顶额外三张牌并选择一名其他角色，其使用其中点数唯一最大的牌，你获得其中点数唯一最小的牌，然后你分配其余牌。",
  ["@[player]chuiqian"] = "鸣谦",

  ["#ls__chuiqian-invoke"] = "鸣谦：你将摸%arg张牌，可以改为亮出牌堆顶的%arg2张牌并选择一名其他角色",
  ["#lvshi__chuiqian-use"] = "鸣谦：你可以使用%arg",

  ["$lvshi__chuiqian1"] = "纵有强权在侧，亦不可失吾风骨。",
  ["$lvshi__chuiqian2"] = "黜邪崇正，何惧之有？",
}

local function findUniqueCards(cardIds)
  if #cardIds == 0 then
      return nil, nil, {}
  end
  -- 统计点数和对应牌ID
  local pointCount = {}
  local pointToCards = {}
  for _, id in ipairs(cardIds) do
      local card = Fk:getCardById(id)
      local point = card.number
      pointCount[point] = (pointCount[point] or 0) + 1
      if not pointToCards[point] then
          pointToCards[point] = {}
      end
      table.insert(pointToCards[point], id)
  end

  -- 找出最小和最大点数
  local minPoint, maxPoint = math.huge, -math.huge
  for point, _ in pairs(pointCount) do
      if point < minPoint then minPoint = point end
      if point > maxPoint then maxPoint = point end
  end

  -- 确定唯一最小和唯一最大的牌
  local uniqueMin = (pointCount[minPoint] == 1) and pointToCards[minPoint][1] or nil
  local uniqueMax = (pointCount[maxPoint] == 1) and pointToCards[maxPoint][1] or nil

  -- 构建其余牌列表
  local others = {}
  for _, id in ipairs(cardIds) do
      if id ~= uniqueMin and id ~= uniqueMax then
        table.insert(others, id)
      end
  end

  return uniqueMax, uniqueMin, others
end

chuiqian:addEffect(fk.BeforeCardsMove, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(chuiqian.name) and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 then
      for _, move in ipairs(data) do
        if move.to == player and move.toArea == Card.PlayerHand and move.moveReason == fk.ReasonDraw then
          event:setCostData(self, move)
          return true
        end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local move = event:getCostData(self) ---@type MoveCardsData
    --return player.room:askToSkillInvoke(player, { skill_name = chuiqian.name, prompt = "#ls__chuiqian-invoke:::"..#move.moveInfo })
    local n = #move.moveInfo + 3
    local result = player.room:askToChoosePlayers(player, {
      targets = player.room:getOtherPlayers(player),
      min_num = 1,
      max_num = 1,
      prompt = "#ls__chuiqian-invoke:::"..#move.moveInfo..":".. n,
      skill_name = chuiqian.name,
    })
    if #result > 0 then
      event:setCostData(self, {tos = result, num = n})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == Card.PlayerHand and move.moveReason == fk.ReasonDraw then
        move.moveInfo = {}
      end
    end
    local to = event:getCostData(self).tos[1]
    room:addTableMarkIfNeed(player, "@[player]chuiqian", to.id)
    room:validateSkill(player, "lvshi__qingwang")
    local cardIds = room:getNCards(event:getCostData(self).num)
    room:turnOverCardsFromDrawPile(player, cardIds, chuiqian.name, true)
    room:delay(200)
    local max, min, others = findUniqueCards(cardIds)
    if max then
      table.removeOne(cardIds, max)
      room:askToUseRealCard(to, {
        pattern = {max},
        skill_name = chuiqian.name,
        prompt = "#lvshi__chuiqian-use:::"..Fk:getCardById(max):toLogString(),
        extra_data = { expand_pile = {max} },
      })
    end
    if min then
      table.removeOne(cardIds, min)
      room:obtainCard(player, min, true, fk.ReasonJustMove)
    end
    if #others > 0 then
      room:askToYiji(player, {
        cards = others,
        skill_name = chuiqian.name,
        min_num = #others,
        max_num = #others,
        expand_pile = cardIds,
      })
    end
    room:cleanProcessingArea(cardIds, chuiqian.name)
  end
})

return chuiqian